package bc

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/bc"
	bcReq "github.com/flipped-aurora/gin-vue-admin/server/model/bc/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/service"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"sync"
)

type BcChannalApi struct {
}

var lock = new(sync.Mutex)

var bcChannalService = service.ServiceGroupApp.BcServiceGroup.BcChannalService

// CreateBcChannal 创建bcChannal表
// @Tags BcChannal
// @Summary 创建bcChannal表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body bc.BcChannal true "创建bcChannal表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /bcChannal/createBcChannal [post]
func (bcChannalApi *BcChannalApi) CreateBcChannal(c *gin.Context) {
	var bcChannal bc.BcChannal
	err := c.ShouldBindJSON(&bcChannal)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	bcChannal.RecordVersion = 0
	if err := bcChannalService.CreateBcChannal(&bcChannal); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteBcChannal 删除bcChannal表
// @Tags BcChannal
// @Summary 删除bcChannal表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body bc.BcChannal true "删除bcChannal表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /bcChannal/deleteBcChannal [delete]
func (bcChannalApi *BcChannalApi) DeleteBcChannal(c *gin.Context) {
	var bcChannal bc.BcChannal
	err := c.ShouldBindJSON(&bcChannal)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := bcChannalService.DeleteBcChannal(bcChannal); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteBcChannalByIds 批量删除bcChannal表
// @Tags BcChannal
// @Summary 批量删除bcChannal表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除bcChannal表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /bcChannal/deleteBcChannalByIds [delete]
func (bcChannalApi *BcChannalApi) DeleteBcChannalByIds(c *gin.Context) {
	var IDS request.IdsReq
	err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := bcChannalService.DeleteBcChannalByIds(IDS); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateBcChannal 更新bcChannal表
// @Tags BcChannal
// @Summary 更新bcChannal表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body bc.BcChannal true "更新bcChannal表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /bcChannal/updateBcChannal [put]
func (bcChannalApi *BcChannalApi) UpdateBcChannal(c *gin.Context) {
	var bcChannal bc.BcChannal
	err := c.ShouldBindJSON(&bcChannal)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	lock.Lock()
	defer lock.Unlock()
	if err := bcChannalService.UpdateBcChannal(bcChannal); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// AddBcChannalPhoneNum 增加取号数量
func (bcChannalApi *BcChannalApi) AddBcChannalPhoneNum(c *gin.Context) {
	var bcChannal bc.BcChannalReq
	err := c.ShouldBindQuery(&bcChannal)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	lock.Lock()
	defer lock.Unlock()
	// 根据项目编号
	var bcChannalData bc.BcChannal
	bcChannalData, err = bcChannalService.GetBcChannal(uint(bcChannal.Id))
	if err != nil {
		global.GVA_LOG.Error("根据id查询渠道失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	}
	*bcChannalData.GetPhoneNum += bcChannal.GetPhoneNum // 增加取码数量
	if err := bcChannalService.UpdateBcChannal(bcChannalData); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// AddBcChannalCodeNum 增加取码数量
func (bcChannalApi *BcChannalApi) AddBcChannalCodeNum(c *gin.Context) {
	var bcChannal bc.BcChannalReq
	err := c.ShouldBindQuery(&bcChannal)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	lock.Lock()
	defer lock.Unlock()
	// 根据项目编号
	var bcChannalData bc.BcChannal
	bcChannalData, err = bcChannalService.GetBcChannal(uint(bcChannal.Id))
	if err != nil {
		global.GVA_LOG.Error("根据id查询渠道失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	}
	*bcChannalData.GetCodeNum += bcChannal.GetCodeNum // 增加取码数量
	if err := bcChannalService.UpdateBcChannal(bcChannalData); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindBcChannal 用id查询bcChannal表
// @Tags BcChannal
// @Summary 用id查询bcChannal表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query bc.BcChannal true "用id查询bcChannal表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /bcChannal/findBcChannal [get]
func (bcChannalApi *BcChannalApi) FindBcChannal(c *gin.Context) {
	var bcChannal bc.BcChannal
	err := c.ShouldBindQuery(&bcChannal)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if rebcChannal, err := bcChannalService.GetBcChannal(bcChannal.ID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"rebcChannal": rebcChannal}, c)
	}
}

// GetBcChannalList 分页获取bcChannal表列表
// @Tags BcChannal
// @Summary 分页获取bcChannal表列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query bcReq.BcChannalSearch true "分页获取bcChannal表列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /bcChannal/getBcChannalList [get]
func (bcChannalApi *BcChannalApi) GetBcChannalList(c *gin.Context) {
	var pageInfo bcReq.BcChannalSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := bcChannalService.GetBcChannalInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}
